
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

/**
 * @description: 算起飞点
 * @author: fan
 * @date: 2021/10/11:9:53
 * @version: 1.0
 */
public class GetStartFly {

    /**
     * 无人机起飞半径 单位为米
     * 无人机无风正常续航 大概 13 KM
     */
    public static final int D = 2000;


    public static final int NUM = 10;

    /**
     * 纬度起点
     */
    public static final int LAT = 5;

    /**
     * 经度起点
     */
    public static final int LON = 113;




    public static void main(String[] args) {

        GetStartFly getStartFly = new GetStartFly();
        List<Point> pointList = getStartFly.createPoint();
        // 航线起点有了
        /*
         要找到最左边 最右边 最上，最下的四个点

         最左边  经度最小的点
         最右边  经度最大的点

          纬度是赤道为0，两极最大为90
         最上  纬度最大的
         最下  纬度最小的

         */

        Point leftPoint = pointList.get(0);
        Point rightPoint = pointList.get(0);
        Point heightPoint = pointList.get(0);
        Point lowPoint = pointList.get(0);

        double left;
        double right;
        double height;
        double low;

        for (Point p : pointList) {
            System.out.println(p);
            left = p.getLon();
            right = p.getLon();
            height = p.getLat();
            low = p.getLat();

            if(left<leftPoint.getLon()){
                leftPoint = p;
            }

            if(right>rightPoint.getLon()){
                rightPoint = p;
            }

            if(height>heightPoint.getLat()){
                heightPoint = p;
            }

            if(low < lowPoint.getLat()){
                lowPoint = p;
            }

        }

        //算一个矩形
        System.out.println("leftPoint :"+leftPoint.getLat()+"," + leftPoint.getLon());
        System.out.println("rightPoint :"+rightPoint.getLat()+"," + rightPoint.getLon());
        System.out.println("heightPoint :"+heightPoint.getLat()+"," + heightPoint.getLon());
        System.out.println("lowPoint :"+lowPoint.getLat()+"," + lowPoint.getLon());

        // 再折半分
        /*

        算矩形的长宽   长度和点所在的位置有关，高度就是纬度差* xxx
        先大概计算一下  40075.04km/360°=111.31955km;

                        40075.04km/180 = 222.63911km

        再看要分为几个矩形
         */
        // 单位米
        double chang = (rightPoint.getLon() - leftPoint.getLon())*111.31955*1000;
        double width = (heightPoint.getLat()-lowPoint.getLat())*222.63911*1000;

        System.out.println(chang);
        System.out.println(width);

        /*
        15600 / 500 = 32
        宽度取整+1 再
         */

        // 折成几个长方形
        int n = 0;
        if(width%D != 0){
            n = (int)(width/D) +1;
        }else{
            n = (int) (width/D);
        }

        // 每个小长方形的宽
        double x = (heightPoint.getLat()-lowPoint.getLat())/n;

        System.out.println("x ==== " + x);

        double[] xn = new double[n];

        // 得到第一个x轴坐标
        xn[0] = heightPoint.getLat() + x/2;

        for (int i = 1; i < n ; i++) {
            xn[i] = xn[i-1] + x;
        }

        // 长方形的边  最高的那个点减宽
        // TODO 为防止double计算不精确，所以最后多减一个数
        double[] ln = new double[n+1];
        ln[0] = heightPoint.getLat() - x;
        for (int i = 1; i <= n; i++) {
            ln[i] = ln[i-1]-x;
        }

        // TODO 到这里，已得到所有的纬度了，现在求需要的经度

        // 起飞点的顺序（按起飞点到）
        // 这里还要把点分开，再排序
        System.out.println("==============****************************============");

        List<List<Point>> total = new ArrayList<>();

        // 90 是最高纬度
        for (int i = 0; i <= n; i++) {

            List<Point> group = new ArrayList<>();

            System.out.println( "===========ln[" + i + "]:" + ln[i]+"=================");
            Iterator<Point> iterator = pointList.iterator();
            while (iterator.hasNext()) {
                Point point = iterator.next();
                // TODO 这里为达到最优解，相邻的两个集合前一个和后一个都没有数据时，应该考虑把两个集合的点重新分配进行计算
                if (point.getLat() >= ln[i]){
                    iterator.remove();
                    group.add(point);
                }
            }
            if(!group.isEmpty()){
                total.add(group);
            }
        }


        /*
         TODO
          集合中只一个点的时候，那个点就是起飞点，
          两个点时看距离是否超过直径，
          三个点时看是否满足一个圆（不满足再求其中两两之间是否满足），
          三个点以上，用贪心算法
         */


        // 算起飞点坐标了




    }

    List<Point> createPoint(){

        List<Point> points = new ArrayList<>(NUM);
//
//        Random random = new Random();
//
//        for (int i = 0; i <NUM ; i++) {
//
//            Point p = new Point();
//            // 5
//            p.setLat(LAT+random.nextDouble());
//            // 113
//            p.setLon(LON+random.nextDouble());
//
//            points.add(p);
//
//        }

        Point p1 = new Point(5.425377359210434, 113.90495750669821);
        Point p2 = new Point(5.333619100059478, 113.61796077153664);
        Point p3 = new Point(5.784611874456655, 113.8718104261144);
        Point p4 = new Point(5.398119885762329, 113.50523456360318);
        Point p5 = new Point(5.204910246012333, 113.12934352386105);
        Point p6 = new Point(5.57857727904457, 113.53887350976653);
        Point p7 = new Point(5.658353050124422, 113.69583494091357);
        Point p8 = new Point(5.274981261182376, 113.97025666696116);
        Point p9 = new Point(5.180406619113166, 113.1434295511145);
        Point p10 = new Point(5.182292619677767, 113.37238609808715);
        points.add(p1);
        points.add(p2);
        points.add(p3);
        points.add(p4);
        points.add(p5);
        points.add(p6);
        points.add(p7);
        points.add(p8);
        points.add(p9);
        points.add(p10);


        return points;
    }

}
